(function (w) {

    var modal = null;
    var callback = null;

    function Modal() {
        this.modal = null;
        this.init();
    }

    Modal.prototype.createModal = function () {
        var that = this;
        var warp = document.createElement("div");
        warp.innerHTML = "" +
            '<div class="settings">' +
            '<div class="modal">' +
            '<div class="modal-title">走势图设置</div>' +
            '<div class="sort-box">' +
            '<div>顺序</div>' +
            '<div class="sort-item sort">' +
            '<button class="sort-btn" id="order">正序</button>' +
            '</div>' +
            '<div class="sort-item desc">' +
            '<button class="sort-btn" id="desc">倒序</button>' +
            '</div>' +
            '</div>' +
            '<div class="btn-bar">' +
            '<div class="btn cancel" id="cancelBtn">取消</div>' +
            '<div class="btn confirm" id="confirmBtn">确认</div>' +
            '</div>' +
            '</div>' +
            '</div>';
        warp.addEventListener("click", this.destroy.bind(this));
        document.body.appendChild(warp);

        document.querySelector(".settings .modal").addEventListener("click", function (e) {
            e.stopPropagation();
        });

        var sortBtns = document.querySelectorAll(".sort-btn");
        var orderBtn = document.querySelector("#order");
        var descBtn = document.querySelector("#desc");
        var cancelBtn = document.querySelector("#cancelBtn");
        var confirmBtn = document.querySelector("#confirmBtn");

        orderBtn.addEventListener("click", function () {
            for (var j = 0; j < sortBtns.length; j++) {
                sortBtns[j].classList.remove("active");
            }
            this.classList.add("active");
        });
        descBtn.addEventListener("click", function () {
            for (var j = 0; j < sortBtns.length; j++) {
                sortBtns[j].classList.remove("active");
            }
            this.classList.add("active");
        });

        cancelBtn.addEventListener("click", function () {
            that.destroy();
        });
        confirmBtn.addEventListener("click", function () {
            var sortType = "order";
            if (orderBtn.classList.contains("active")) {
                sortType = "order"
            } else {
                sortType = "desc";
            }
            callback && callback({
                sortType: sortType
            });
            that.destroy();
        });

        if (global.sortType === "order") {
            orderBtn.classList.add("active");
        } else {
            descBtn.classList.add("active");
        }

        return warp;
    };

    Modal.prototype.init = function () {
        this.modal = this.createModal();
    };

    Modal.prototype.destroy = function () {
        document.body.removeChild(this.modal);
    };

    w.Modal = function (fn) {
        modal = new Modal();
        callback = fn;
        return modal;
    }
})(window);
